perf: optimize arrays_zip perfect list zips#22285
Open
puneetdixit200 wants to merge 4 commits into
Open
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Adds a fast-path to arrays_zip that can reuse existing offsets and value buffers when inputs are “perfectly aligned” list arrays, and updates benchmarks accordingly.
Changes:
- Introduce
try_perfect_list_zipfast-path and invoke it early fromarrays_zip_inner - Add unit tests covering reuse behavior and fallback conditions
- Rename the no-nulls benchmark case to reflect the new “perfect zip” scenario
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| datafusion/functions-nested/src/arrays_zip.rs | Adds perfect-zip fast-path plus tests validating reuse and fallback behavior |
| datafusion/functions-nested/benches/arrays_zip.rs | Renames benchmark to better represent the new fast-path scenario |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+360
to
+397
| for arr in &list_arrays { | ||
| if arr.len() != num_rows || arr.values().len() != values_len { | ||
| return Ok(None); | ||
| } | ||
| } | ||
|
|
||
| let nulls = if list_arrays.iter().any(|arr| arr.null_count() != 0) { | ||
| let mut null_builder = NullBufferBuilder::new(num_rows); | ||
| for row_idx in 0..num_rows { | ||
| let mut all_null = true; | ||
|
|
||
| for arr in &list_arrays { | ||
| if arr.is_null(row_idx) { | ||
| if arr.offsets()[row_idx + 1] != arr.offsets()[row_idx] { | ||
| return Ok(None); | ||
| } | ||
| } else { | ||
| all_null = false; | ||
| } | ||
| } | ||
|
|
||
| if all_null { | ||
| null_builder.append_null(); | ||
| } else { | ||
| null_builder.append_non_null(); | ||
| } | ||
| } | ||
|
|
||
| null_builder.finish() | ||
| } else { | ||
| None | ||
| }; | ||
|
|
||
| for arr in &list_arrays { | ||
| if arr.offsets() != &offsets { | ||
| return Ok(None); | ||
| } | ||
| } |
Comment on lines
+335
to
+350
| fn try_perfect_list_zip(args: &[ArrayRef]) -> Result<Option<ArrayRef>> { | ||
| let mut list_arrays = Vec::with_capacity(args.len()); | ||
| let mut struct_fields = Vec::with_capacity(args.len()); | ||
|
|
||
| for (i, arg) in args.iter().enumerate() { | ||
| let arr = match arg.data_type() { | ||
| List(field) => { | ||
| struct_fields.push(Field::new( | ||
| format!("{}", i + 1), | ||
| field.data_type().clone(), | ||
| true, | ||
| )); | ||
| as_list_array(arg)? | ||
| } | ||
| _ => return Ok(None), | ||
| }; |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Which issue does this PR close?
arrays_zipto avoid row-by-row copying in the perfect-zip case #22225.Rationale for this change
arrays_zipcurrently uses the generalMutableArrayDatapath even when all regularListArrayinputs are already perfectly aligned. In that case, the output list offsets match the inputs and each struct child column can reuse the corresponding input values array instead of copying one row at a time.What changes are included in this PR?
ListArrayzips that reuses the first input's offsets and clones the input values arrays into the output struct children.LargeList,FixedSizeList,Nullinputs, and null rows that would require padding.arrays_zip_perfect_zip_8192.Are these changes tested?
Yes. Local checks run:
cargo fmt --allcargo test -p datafusion-functions-nestedcargo clippy -p datafusion-functions-nested --all-targets --all-features -- -D warningsCARGO_TARGET_DIR=C:\df-target cargo clippy --all-targets --all-features -- -D warningsCARGO_TARGET_DIR=C:\df-target cargo bench -p datafusion-functions-nested --bench arrays_zip -- --warm-up-time 1 --measurement-time 2 --sample-size 10Latest local benchmark sample:
arrays_zip_perfect_zip_8192:11.234 µs 11.600 µs 12.045 µsarrays_zip_10pct_nulls_8192:4.3463 ms 4.5531 ms 4.7898 msAre there any user-facing changes?
No. This is an internal performance optimization with the same
arrays_zipoutput semantics.## Which issue does this PR close?arrays_zipto avoid row-by-row copying in the perfect-zip case #22225.Rationale for this change
arrays_zipcurrently uses the generalMutableArrayDatapath even when all regularListArrayinputs are already perfectly aligned. In that case, the output list offsets match the inputs and each struct child column can reuse the corresponding input values array instead of copying one row at a time.What changes are included in this PR?
ListArrayzips that reuses the first input's offsets and clones the input values arrays into the output struct children.LargeList,FixedSizeList,Nullinputs, and null rows that would require padding.arrays_zip_perfect_zip_8192.Are these changes tested?
Yes. Local checks run:
cargo fmt --allcargo test -p datafusion-functions-nestedcargo clippy -p datafusion-functions-nested --all-targets --all-features -- -D warningsCARGO_TARGET_DIR=C:\df-target cargo clippy --all-targets --all-features -- -D warningsCARGO_TARGET_DIR=C:\df-target cargo bench -p datafusion-functions-nested --bench arrays_zip -- --warm-up-time 1 --measurement-time 2 --sample-size 10Latest local benchmark sample:
arrays_zip_perfect_zip_8192:11.234 µs 11.600 µs 12.045 µsarrays_zip_10pct_nulls_8192:4.3463 ms 4.5531 ms 4.7898 msAre there any user-facing changes?
No. This is an internal performance optimization with the same
arrays_zipoutput semantics.